/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * formThongTinKhachHang.java
 *
 * Created on Jun 22, 2013, 3:41:45 PM
 */
package GUI;

import BUS.KhachHangBUS;
import Entity.KhachHang;
import Util.ComboBoxTagModel;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author copang
 */
public class formThongTinKhachHang extends javax.swing.JPanel {

    private KhachHangBUS khachHangBUS;
    private ComboBoxTagModel cmbGioiTinhModels = null;
    private String[] columnNames;
    private Object[][] data;

    /**
     * Creates new form formThongTinKhachHang
     */
    public formThongTinKhachHang() {
        initComponents();

        khachHangBUS = new BUS.KhachHangImpl();
        txtDienThoai.setTransferHandler(null);
        txtStatus.setText("");
        columnNames = new String[]{"Mã khách hàng", "Tên khách hàng", "Tiền nợ", "Giới tính",
            "Địa chỉ", "Điện thoại", "Email"};

        loadData();
    }

    protected void loadData() {

        cmbGioiTinhModels = new ComboBoxTagModel();
        cmbGioiTinhModels.addElement("Nam", 0);
        cmbGioiTinhModels.addElement("Nữ", 1);
        ArrayList list;

        // get list Khach hang
        list = khachHangBUS.getKhachHang();
        data = new Object[list.size()][7];
        for (int i = 0; i < list.size(); i++) {
            KhachHang kh = (KhachHang) list.get(i);
            data[i][0] = String.valueOf(kh.getMaKhachHang());
            data[i][1] = kh.getHoTenKH();
            data[i][2] = String.valueOf(kh.getSoTienNo());
            data[i][3] = kh.getGioiTinh().toString() == "true" ? "Nam" : "Nữ";
            data[i][4] = kh.getDiaChiKH();
            data[i][5] = kh.getDienThoaiKH();
            data[i][6] = kh.getEmailKH();
        }
        tableKH.setModel(new DefaultTableModel(data, columnNames));
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txtEmail = new javax.swing.JTextField();
        txtDiaChi = new javax.swing.JTextField();
        txtDienThoai = new javax.swing.JTextField();
        jLabel20 = new javax.swing.JLabel();
        cmbGioiTinh = new javax.swing.JComboBox();
        txtTenKH = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableKH = new javax.swing.JTable();
        jLabel16 = new javax.swing.JLabel();
        jLabel17 = new javax.swing.JLabel();
        jLabel18 = new javax.swing.JLabel();
        jLabel23 = new javax.swing.JLabel();
        jLabel21 = new javax.swing.JLabel();
        txtTienNo = new javax.swing.JTextField();
        txtStatus = new javax.swing.JLabel();
        jToolBar1 = new javax.swing.JToolBar();
        btnAddKH = new javax.swing.JButton();
        btnXoaKH = new javax.swing.JButton();
        btnEditKH = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        txtEmail.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N

        txtDiaChi.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N

        txtDienThoai.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        txtDienThoai.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtDienThoaiKeyTyped(evt);
            }
        });

        jLabel20.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        jLabel20.setText("Email");

        cmbGioiTinh.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        cmbGioiTinh.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Nam", "Nữ" }));

        txtTenKH.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N

        tableKH.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null}
            },
            new String [] {
                "Mã khách hàng", "Tên khách hàng", "Tiền nợ", "Giới tính", "Địa chỉ", "Điện thoại", "Email"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tableKH.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tableKHMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tableKH);

        jLabel16.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        jLabel16.setText("Địa chỉ");

        jLabel17.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        jLabel17.setText("Giới tính");

        jLabel18.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        jLabel18.setText("Tên khách hàng");

        jLabel23.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        jLabel23.setText("Tiền nợ");

        jLabel21.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        jLabel21.setText("Điện thoại");

        txtTienNo.setEditable(false);
        txtTienNo.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N

        txtStatus.setFont(new java.awt.Font("Arial", 3, 14)); // NOI18N
        txtStatus.setForeground(new java.awt.Color(255, 0, 0));
        txtStatus.setText("status");

        jToolBar1.setRollover(true);

        btnAddKH.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        btnAddKH.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/Add.png"))); // NOI18N
        btnAddKH.setText("Thêm KH");
        btnAddKH.setFocusable(false);
        btnAddKH.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btnAddKH.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnAddKH.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddKHActionPerformed(evt);
            }
        });
        jToolBar1.add(btnAddKH);

        btnXoaKH.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        btnXoaKH.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/delete.png"))); // NOI18N
        btnXoaKH.setText("Xóa KH");
        btnXoaKH.setFocusable(false);
        btnXoaKH.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btnXoaKH.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnXoaKH.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnXoaKHActionPerformed(evt);
            }
        });
        jToolBar1.add(btnXoaKH);

        btnEditKH.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        btnEditKH.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/edit.png"))); // NOI18N
        btnEditKH.setText("Sửa thông tin");
        btnEditKH.setFocusable(false);
        btnEditKH.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btnEditKH.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnEditKH.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditKHActionPerformed(evt);
            }
        });
        jToolBar1.add(btnEditKH);

        jButton1.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/update.png"))); // NOI18N
        jButton1.setText("Cập Nhật");
        jButton1.setFocusable(false);
        jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });
        jToolBar1.add(jButton1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(38, 38, 38)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel17)
                            .addComponent(jLabel18)
                            .addComponent(jLabel16))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(txtTenKH, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 231, Short.MAX_VALUE)
                                .addComponent(txtDiaChi, javax.swing.GroupLayout.Alignment.LEADING))
                            .addComponent(cmbGioiTinh, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(59, 59, 59)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel21)
                            .addComponent(jLabel20)
                            .addComponent(jLabel23))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(txtEmail, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 175, Short.MAX_VALUE)
                            .addComponent(txtDienThoai, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtTienNo)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(30, 30, 30)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 803, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(txtStatus)))
                .addGap(26, 26, 26))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel18)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtTenKH, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel21)
                            .addComponent(txtDienThoai, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtDiaChi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel16, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel20)
                            .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cmbGioiTinh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel17)
                            .addComponent(jLabel23)
                            .addComponent(txtTienNo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(31, 31, 31)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 306, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(txtStatus)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    protected void xoaKH() {
        int maKH = Integer.parseInt(tableKH.getModel().getValueAt(tableKH.getSelectedRow(), 0).toString());
        if (khachHangBUS.delete(maKH)) {
            txtTenKH.setText("");
            txtDienThoai.setText("");
            txtDiaChi.setText("");
            txtEmail.setText("");
            txtTienNo.setText("");
            txtStatus.setText("Xóa khách hàng thành công");
            loadData();
        } else {
            JOptionPane.showMessageDialog(this, "Xóa khách hàng không thành công");
        }
    }

    protected void suaKH() {
        int manv = Util.InstanceData.loginNhanVien.getMaNhanVien();
        boolean isNam = (cmbGioiTinh.getSelectedItem().toString() == "Nam") ? true : false;
        KhachHang newKH = new KhachHang(Integer.parseInt(tableKH.getModel().getValueAt(tableKH.getSelectedRow(), 0).toString()),
                manv, txtTenKH.getText(), isNam, txtDiaChi.getText(), txtDienThoai.getText(),
                txtEmail.getText(), Integer.parseInt(txtTienNo.getText()));

        if (khachHangBUS.edit(newKH)) {
            txtTenKH.setText("");
            txtDienThoai.setText("");
            txtDiaChi.setText("");
            txtEmail.setText("");
            txtTienNo.setText("");
            txtStatus.setText("Sửa khách hàng thành công");
            loadData();
        } else {
            JOptionPane.showMessageDialog(this, "Sửa khách hàng không thành công");
        }
    }

        private void tableKHMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tableKHMouseClicked

            txtTenKH.setText(tableKH.getModel().getValueAt(tableKH.getSelectedRow(), 1).toString());
            txtDienThoai.setText(tableKH.getModel().getValueAt(tableKH.getSelectedRow(), 5).toString());
            txtDiaChi.setText(tableKH.getModel().getValueAt(tableKH.getSelectedRow(), 4).toString());
            txtEmail.setText(tableKH.getModel().getValueAt(tableKH.getSelectedRow(), 6).toString());
            txtTienNo.setText(tableKH.getModel().getValueAt(tableKH.getSelectedRow(), 2).toString());
            int idx = tableKH.getModel().getValueAt(tableKH.getSelectedRow(), 3).toString() == "Nam" ? 0 : 1;
            cmbGioiTinh.setSelectedIndex(idx);
        }//GEN-LAST:event_tableKHMouseClicked

        private void txtDienThoaiKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtDienThoaiKeyTyped
            Util.InstanceData.check(evt);
        }//GEN-LAST:event_txtDienThoaiKeyTyped

    private void btnAddKHActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddKHActionPerformed
        addKH();
    }//GEN-LAST:event_btnAddKHActionPerformed

    private void btnXoaKHActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnXoaKHActionPerformed
        xoaKH();
    }//GEN-LAST:event_btnXoaKHActionPerformed

    private void btnEditKHActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditKHActionPerformed
        suaKH();
    }//GEN-LAST:event_btnEditKHActionPerformed

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked
        loadData();
    }//GEN-LAST:event_jButton1MouseClicked

    protected void addKH() {
        int manv = Util.InstanceData.loginNhanVien.getMaNhanVien();
        boolean isNam = (cmbGioiTinh.getSelectedItem().toString() == "Nam") ? true : false;
        KhachHang newKH = new KhachHang(0, manv, txtTenKH.getText(), isNam,
                txtDiaChi.getText(), txtDienThoai.getText(), txtEmail.getText(),
                0);

        if (khachHangBUS.add(newKH)) {
            txtTenKH.setText("");
            txtDienThoai.setText("");
            txtDiaChi.setText("");
            txtEmail.setText("");
            txtTienNo.setText("");
            txtStatus.setText("Thêm khách hàng thành công");
            loadData();
        } else {
            JOptionPane.showMessageDialog(this, "Thêm khách hàng không thành công");
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAddKH;
    private javax.swing.JButton btnEditKH;
    private javax.swing.JButton btnXoaKH;
    private javax.swing.JComboBox cmbGioiTinh;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel21;
    private javax.swing.JLabel jLabel23;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JTable tableKH;
    private javax.swing.JTextField txtDiaChi;
    private javax.swing.JTextField txtDienThoai;
    private javax.swing.JTextField txtEmail;
    private javax.swing.JLabel txtStatus;
    private javax.swing.JTextField txtTenKH;
    private javax.swing.JTextField txtTienNo;
    // End of variables declaration//GEN-END:variables
}
